#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<string.h>
#include<map>
using namespace std;
typedef struct node{
    string effective_character; //有效的输入
    string statue;              //输入对应的状态
}status;
void init(string& effective_character,string& state,map<char,status> &state_map,const char* s,vector<string> &data){
    //对输入的string类型进行分割
    //effective_character 有效的输入
    //state有效的状态
    //状态map,根据字符串，找到对应的状态结构体
    //输入的字符串
    //将要匹配的串
    int i=0;
    //第一行为有效的输入字符集
    while(s[i]!='#'){
        if(s[i]!=' ')
         effective_character+=s[i];
        i++;
    }
    i++;
    while(s[i]=='\n'){
        i++;
    }//过滤换行符
    //第二行为有效状态集
    while(s[i]!='#'){
        if(s[i]!=' ')
         state+=s[i];
        i++;
    }
    i++;
    while (s[i] == '\n') {
        i++;
    }
    int j = 0;
    //状态转移
    while (j < state.length()) {
        string temp_str="";
        string temp_str2 = "";
        char a = s[i];
        i++;
        while (s[i] != '\n') {
            if (s[i] == '>') {
                temp_str += s[i + 1];
                temp_str2 += s[i - 2];
            }
            i++;
        }
        status new_status = { temp_str2,temp_str };
        state_map.insert(pair <char,status> (a,new_status));
        j++;
        while (s[i] == '\n') {
            i++;
        }
    }
    while (s[i] == '\n') {
        i++;
    }
    //输入集
    while (i<strlen(s)) {
        string temp = "";
        while (s[i] != '#') {
            temp += s[i];
            i++;
        }
        temp += s[i]; 
        i++;
        data.push_back(temp);
        while (s[i] == '\n') {
            i++;
        }
    }
}
string getFromTxt(){
    //从文件中读取所有的数据，并返回一个string类型。
    string date;
    ifstream inFile;
    inFile.open("in4.txt",ios::in);
    if(inFile){
        //printf("文件打开成功，正在读取数据\n");
        string buffer;
        // stringstream buffer;            // stringstream object
        // buffer << inFile.rdbuf();
        while(getline(inFile,buffer)){
            date+=buffer;
            date+='\n';
        }
       // printf("数据读取完毕\n");
        inFile.close();
       // printf("文件关闭\n");
    }
    //cout<<date;
    return date;
}
string getFromUser(){
    //从读取用户所有输入，并返回一个string类型。
    string date;
    string buffer;
    while(getline(cin,buffer)){
        date+=buffer;
        date+='\n';
    }
    //cout << date;
    return date;
}
void panduan(string identify_str,string effect_character,map<char,status>state_map) {
    //identify_str是待识别单词
    int i = 0;
    char now_status = 'X';
    while (identify_str[i] != '#') {
        char  c = identify_str[i];
        string::size_type idx = state_map[now_status].effective_character.find(c);
        if (idx == string::npos) {
            cout << "error"<<endl;
            return;
        }
        now_status = state_map[now_status].statue[idx];
        cout << c << endl;
        i++;
    }
    if (now_status == 'Y') {
        cout << "pass" << endl;
    }
    else {
        cout << "error" << endl;
    }
}
int main(){
    string effective_character;
    string state;
    string str = getFromTxt();
    vector<string> data;
    map<char, status> state_map;
    const char * cstr=str.data();
    //cout<<cstr<<endl;
    init(effective_character,state,state_map,cstr,data);
    for (int i = 0; i < data.size(); i++) {
        panduan(data[i], effective_character, state_map);
    }
    //getFromUser();
}